import { getFormattedTime } from "@/func";
import { Difficulty, GameMode, world } from "@minecraft/server";
import { InfoScoreboard } from "@sapi-game/gameComponent/view/infoScoreboard";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { PillarOfFortuneBorder } from "../components/border";
import { PillarOfFortuneDaemon } from "../components/daemon";
import { PillarOfFortuneDamage } from "../components/damage";
import { PillarOfFortuneEventActuator } from "../components/eventActuator";
import { PillarOfFortuneEvent } from "../components/events";
import { PillarOfFortunePlayerLimiter } from "../components/playerLimiter";
import { PillarOfFortuneRandomItem } from "../components/randomItem";
import { PillarOfFortuneRespawn } from "../components/respawn";
import { PillarOfFortuneWatingRoom } from "../components/waitingRoom";
import { PillarOfFortuneModule } from "../module";
import { PillarOfFortuneEndState } from "./endState";

export class PillarOfFortuneMainState extends PillarOfFortuneModule.State {
    override onEnter(): void {
        this.addComponent(Timer, {
            initialTime: 1200,
            autoStart: true,
            compensate: true,
        });
        this.addComponent(PillarOfFortuneWatingRoom)
            .addComponent(PillarOfFortunePlayerLimiter)
            .addComponent(InfoScoreboard, {
                scoreBoardName: "pillarOfForture_" + this.engine.key,
                displayName: "§a§l幸运之柱",
                showOnAttach: true,
                paddingLeft: 2,
                header: () => ["§7" + getFormattedTime()],
                footer: () => ["§d§lPartyGames"],
            })
            .addComponent(PillarOfFortuneRandomItem, { interval: 4 }) //给物品
            .addComponent(PillarOfFortuneBorder) //边界
            .addComponent(PillarOfFortuneDamage) //记录伤害玩家
            .addComponent(PillarOfFortuneRespawn) //重生
            .addComponent(PillarOfFortuneDaemon)
            .addComponent(PillarOfFortuneEventActuator)
            .addComponent(PillarOfFortuneEvent);
        this.init();
    }

    init() {
        //摧毁开始房间
        world.gameRules.pvp = true;
        world.gameRules.fallDamage = true;
        world.setDifficulty(Difficulty.Easy);
        this.getComponent(PillarOfFortuneWatingRoom).destroyWatingRoom();
        this.context.players.forEach((p) => {
            p.player.setGameMode(GameMode.Survival);
        });
    }

    end() {
        this.transitionTo(PillarOfFortuneEndState);
    }

    override onExit(): void {
        world.gameRules.fallDamage = false;
        world.setDifficulty(Difficulty.Peaceful);
    }
}
